/*============================================================================*/
/*                              x embedded systems                            */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* %name:            sw_timers.c%
* %version:         1.0%
* %created_by:      David Robles%
* %date_created:    March 14 2012%
*=============================================================================*/
/* DESCRIPTION : Timers by software                                           */
/*============================================================================*/
/* FUNCTION COMMENT : Provide APIs to use software timers                     */
/*                                                                            */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 14/03/2012  |                               | David Robles     */
/* File added to project                                                      */
/*============================================================================*/

/* Includes */
/* -------- */
#include "stdtypedef.h"
#include "p18f4550.h"
#include "hw_timers.h"
#include "sw_timers.h"

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 
/* BYTE constants */
const T_UBYTE cub_TotalTimers = SW_TIMERS_N_TIMERS;

/* WORD constants */
const T_UWORD cuw_CaptureTimer_init = 0;

/* LONG and STRUCTURE constants */

/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE RAM variables */

/* WORD RAM variables */
T_UWORD ruw_CaptureTimer[SW_TIMERS_N_TIMERS];

/* LONG and STRUCTURE RAM variables */



/* Private functions prototypes */
/* ---------------------------- */
//PRIVATE_FCT void priv_func (void);


/* Exported functions prototypes */
/* ----------------------------- */
/* prototypes */
void sw_timers_InitTimers(void);
void sw_timers_StartTimer(T_UBYTE lub_TimerNum);
void sw_timers_StopTimer(T_UBYTE lub_TimerNum);
T_UWORD sw_timers_CheckTimer(T_UBYTE lub_TimerNum);
T_UBYTE sw_timers_IsExpiredTimer(T_UBYTE lub_TimerNum, T_UWORD luw_Time);
T_UBYTE sw_timers_IsFreeTimer(T_UBYTE lub_TimerNum);

/* +++++++++++++++++++++ APIs ++++++++++++++++++++++ */
/**************************************************************
 *  Name                 : sw_timers_InitTimers
 *  Description          : Initialize all Software Timers
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void sw_timers_InitTimers(void)
{
	T_UBYTE lub_TimerNum;
	
	for(lub_TimerNum = 0; lub_TimerNum < cub_TotalTimers; lub_TimerNum++)
	{
    	ruw_CaptureTimer[lub_TimerNum] = cuw_CaptureTimer_init;
 	}   
}

/**************************************************************
 *  Name                 : sw_timers_StartTimer
 *  Description          : Start Timer N
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void sw_timers_StartTimer(T_UBYTE lub_TimerNum)
{
    ruw_CaptureTimer[lub_TimerNum] = Get_BaseTime();
}

/**************************************************************
 *  Name                 : sw_timers_StopTimer
 *  Description          : Stop Timer N
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void sw_timers_StopTimer(T_UBYTE lub_TimerNum)
{
    ruw_CaptureTimer[lub_TimerNum] = cuw_CaptureTimer_init;
}

/**************************************************************
 *  Name                 : sw_timers_StopTimer
 *  Description          : Return the current time elpsed from the selected timer
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
T_UWORD sw_timers_CheckTimer(T_UBYTE lub_TimerNum)
{
    T_UWORD luw_TimerValue;
    T_UWORD luw_CopyTimeBase;
    
	luw_CopyTimeBase = Get_BaseTime();
    if (ruw_CaptureTimer[lub_TimerNum] > luw_CopyTimeBase)
	{
	    luw_TimerValue = _MAX_UWORD_ - ruw_CaptureTimer[lub_TimerNum] + luw_CopyTimeBase;
	}
	else
	{
	    luw_TimerValue = luw_CopyTimeBase - ruw_CaptureTimer[lub_TimerNum];
	}
    return luw_TimerValue;
}

/**************************************************************
 *  Name                 : IsExpiredDelay
 *  Description          : Returns if the delay has expired
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
T_UBYTE sw_timers_IsExpiredTimer(T_UBYTE lub_TimerNum, T_UWORD luw_Time)
{
	T_UBYTE lub_Expired;

    if (sw_timers_CheckTimer(lub_TimerNum) > luw_Time)
	{
	    lub_Expired = TRUE;
	}
	else
	{
	    lub_Expired = FALSE;
    }
	return lub_Expired;
}

/**************************************************************
 *  Name                 : IsRunningDelay
 *  Description          : Returns if the delay is running
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
T_UBYTE sw_timers_IsFreeTimer(T_UBYTE lub_TimerNum)
{
	T_UBYTE lub_Free;

    if (cuw_CaptureTimer_init == ruw_CaptureTimer[lub_TimerNum])
	{
	    lub_Free = TRUE;
	}
	else
	{
	    lub_Free = FALSE;
    }
	return lub_Free;
}
